package com.hemaapp.yjnh.activity;

import xtom.frame.util.XtomToastUtil;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;

/**
 * 信息编辑页面
 * @params requestCode 请求的code
 * @params name 标题名称
 * @params value 值
 * requestCode = 2:只能输入数字
 * requestCode = R.id.linearlayout:允许输入小数
 * requsetCode = R.id.gridview:输入电话号码
 */
public class SetInputActivity extends BaseActivity {
	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private EditText editText;

	private int requestCode;
	private String name;
	private String value;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_setinput);
		super.onCreate(savedInstanceState);
		titleText.setText(name);
		if (!isNull(value)) {
			editText.setText(value);
			editText.setSelection(value.length());
		}
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
	}

	@Override
	protected void findView() {
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleText = (TextView) findViewById(R.id.title_text);
        titleRight = (Button) findViewById(R.id.title_right_btn);

		editText = (EditText) findViewById(R.id.edittext);
	}

	@Override
	protected void getExras() {
		requestCode = mIntent.getIntExtra("requestCode", 0);
		name = mIntent.getStringExtra("name");
		value = mIntent.getStringExtra("value");
	}

	@Override
	protected void setListener() {
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.VISIBLE);
		titleRight.setText("保存");

		switch (requestCode) {
		case 2: 
			editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
			break;
		case R.id.linearlayout:
			editText.setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
			break;
		case R.id.gridview:
			editText.setInputType(EditorInfo.TYPE_CLASS_PHONE);
			break;
		default:
			break;
		}

		editText.setHint("请输入" + name);

		OnTextChangeListener l = new OnTextChangeListener();
		editText.addTextChangedListener(l);
		titleRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String content = editText.getText().toString();
				if (isNull(content)) {
					XtomToastUtil.showShortToast(mContext, "内容不能为空，请重新填写");
					return;
				}

				switch (requestCode) {
				case R.id.gridview:
					if (content.length() != 11) {
						XtomToastUtil.showShortToast(mContext, "联系方式请填写手机号");
						return;
					}
					break;
				default:
					mIntent.putExtra("backdata", content);
					setResult(RESULT_OK, mIntent);
					finish();
					break;
				}

			}
		});
		
	}

	private void checkNextable() {
		String content = editText.getText().toString();
		boolean can = !isNull(content);
		if (can)
			titleRight.setEnabled(true);
		else
			titleRight.setEnabled(false);
	}

	private class OnTextChangeListener implements TextWatcher {

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			checkNextable();
		}

	}

}
